const app = getApp();
var util = require('../../../utils/util.js');
var api = require('../../../config/api.js');
var user = require('../../../services/user.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    institutionInfo:{
      address: "",
      brief: "",
      city: null,
      city_id: 0,
      province: null,
      province_id: 0,
      county_id: 0,

      contact: "",
      establishment_time: null,
      institution_id: null,
      introduce: "",
      
      map: null,
      member_id: null,
      mobile: "",
      name: "",

      qq: "",
      tel: "",
      train_type_id: 0,
      'type': null,
      updated_at: null,
      website: "",

      logo: "",
      wechat_ercode: "",
    },
    // logo: false,
    // wechat_ercode: false,

    // name:"",
    // train_type_id:"", 
    // contact:"",
    // tel:"",
    // mobile: "",
    // address:"",
    // introduce:"",

    region: ['湖南省', '长沙市', '岳麓区'],
    customItem: '全部',

    index: 0,
    type_name:"",
    train_type_id:0,
    trainTypeList:[],
    userInfo:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var typeList = wx.getStorageSync("trainType")
    var trainTypeList = new Array();
    for (var item in typeList[112].sub){
      trainTypeList.push(typeList[112].sub[item])
    }    
    this.setData({
      trainTypeList: trainTypeList,
      userInfo: wx.getStorageSync("userInfo")
    })

    this.getJigou();
  },
  bindTrainTypeChange:function(e){    
    var selectType = this.data.trainTypeList[e.detail.value]
    console.log(selectType)
    this.setData({
      index:e.detail.value,
      type_name: selectType.type_name,
      train_type_id: selectType.train_type_id
    })
    console.log(e,this.data.index)
  }, 
  bindRegionChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      region: e.detail.value
    })
  },
  getJigou:function(){
    let that = this;
    util.request(api.InstitutionDetail, { member_id: that.data.userInfo.member_id }).then(res => {
      // if (res.code === 200) {
      //   that.setData({ institutionInfo: res.data.institution})
      // }      
      if(res.code == 200 ){
        wx.redirectTo({
          url: '/pages/user/jigou/index',
        })
      }
      wx.hideLoading();
    }).catch(() => {      
      wx.hideLoading();
    });
  },
  formSubmit:function(e){
    var data = e.detail.value;
    data.logo = this.data.institutionInfo.logo;
    data.wechat_ercode = this.data.institutionInfo.wechat_ercode
    data.region = this.data.region
    data.member_id = this.data.userInfo.member_id;
    data.train_type_id = this.data.train_type_id;
    console.log(data)
    util.request(api.RuzhuInstitution,data,"POST").then(res=>{
      console.log(res)
      if (res.code == 200){
        wx.showToast({
          title: '申请成功！请等待审核',
          duration: 2000,
          success:function(){
            wx.redirectTo({
              url: '/pages/user/jigou/index',
            })
          }
        })
        
      }
      
    });
  },
  chooseImage: function (e) {
    var that = this;
    var field = e.currentTarget.dataset.field
    wx.chooseImage({
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: api.ImageUpload,
          filePath: tempFilePaths[0],
          header: {
            'Content-Type': 'application/json'
          },
          name: 'file',
          formData:{
            token: wx.getStorageSync("token")
          },
          success: function (res) {
            var data = JSON.parse(res.data);
            if(data.code == 200){              
              if(field == "logo"){
                that.setData({
                  'institutionInfo.logo': data.data.url
                });
              }else{
                that.setData({
                  'institutionInfo.wechat_ercode': data.data.url
                });
              }
              
            }
          }
        })        
      }
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})